package qing.controller.cms;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

import qing.entity.Node;
import qing.service.NodeService;
import qing.service.TplService;



@Controller
@RequestMapping("/cms/node")
public class NodeController {

	@Autowired
	private NodeService idao;
	
	@Autowired
	private TplService itpldao;
	
	@ResponseBody
	@RequestMapping("list")
	public String showlists() {
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Node.class, "id,title,call,taxis,pid,layer,deleted"); 
		List<Node> list= idao.list("from Node");
		String json = JSON.toJSONString(list, filter);
		return json;
	}
	
	//绑定会员模型
	@ModelAttribute
	public void getnode(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			System.out.println("modeAttribute");
			map.put("node", idao.find(Node.class, id));
		}
	}
	
//添加用户显示
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addget(Map<String, Object> map) {
		map.put("node",new Node());
		map.put("tpl1", itpldao.getddllist(2));
		map.put("tpl2", itpldao.getddllist(3));
		return "cms/article/addnode";
	}
 //提交新增
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String addpost(@Valid Node u, BindingResult result, Map<String, Object> map) {		
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}
			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("node",u);
			return "addnode";
		}
		idao.create(u);
		return "redirect:list";
	}
//修改初始化
	@RequestMapping(value = "add/{id}", method = RequestMethod.GET)
	public String editinit(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("node", idao.find(Node.class, id));
		return "addnode";
	}  
//提交修改
	@RequestMapping(value = "add", method = RequestMethod.PUT)
	public String eidtpost(@Valid Node u, BindingResult result, Map<String, Object> map) {
		System.out.println("开始修改，并重定向……" + u.toString());
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}

			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("node",u);
			return "addnode";
		}
				
		idao.update(u);
		return "redirect:list";
	}
//删除用户
	@RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		System.out.println("dele:" + id);
		idao.delete(Node.class, id);
		return "redirect:../list";
	}


	
	

}
